package ee.maoinvest.frontend.controller.formstringdataconverters;

import java.util.HashMap;

public class FormDataConverter 
{
	public HashMap<String, String> formatErrors;
	protected final int HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS = 0;
	public HashMap<String, String> businessErrors;
	private boolean isDataAfterFormattingOK = false;
	
	public String getErrorByField(String fieldToGetInformationAbout) 
	{
		String fieldError = "";
		String businessErrorExemplar;
		String formatErrorExemplar;
		String WHITESPACE = " ";

		if (formatErrors != null)
		{
			formatErrorExemplar = (String) formatErrors.get(fieldToGetInformationAbout);
			if (formatErrorExemplar != null)
			{
				fieldError = formatErrorExemplar;
			}

		}

		if (businessErrors != null)
		{
			businessErrorExemplar = (String) businessErrors.get(fieldToGetInformationAbout);
			if (businessErrorExemplar != null)
			{
				fieldError = fieldError + WHITESPACE + businessErrorExemplar;
			}

		}
		return fieldError;
	}
	
	
	public boolean isNoErrors()
	{
		if (formatErrors == null && businessErrors == null)
		{
			return true;
		}
		else if (formatErrors.size() == HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS
				&& businessErrors.size() == HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS)
		{
			return true;
		}
		else if (formatErrors == null
				&& businessErrors.size() == HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS)
		{
			return true;
		}
		else if (formatErrors.size() == HASMHAP_SIZE_ZERO_INDICATING_THAT_THERE_WERE_NO_ERRORS
				&& businessErrors == null)
		{
			return true;
		}
		else
		{
			return false; //meaning that there are some errors!
		}
	}
	
	public HashMap<String, String> getBusinessErrors() 
	{
		return businessErrors;
	}

	public void setBusinessErrors(HashMap<String, String> businessErrors) 
	{
		this.businessErrors = businessErrors;
	}

	public HashMap<String, String> getFormatErrors() 
	{
		if (formatErrors == null)
		{
			formatErrors = new HashMap<String, String>();
			return formatErrors;
		}
		else
		{
			return formatErrors;
		}
	}

	public void setFormatErrors(HashMap<String, String> formatErrors) 
	{
		this.formatErrors = formatErrors;
	}
	
	public boolean getIsDataAfterFormattingOK() 
	{
		return isDataAfterFormattingOK;
	}

	public void setIsDataAfterFormattingOK(boolean isDataAfterFormattingOK)
	{
		this.isDataAfterFormattingOK = isDataAfterFormattingOK;
	}
}
